<template>
  <div>
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '80px' } }">
        <a-form-item name="zjm">
          <a-input v-model:value="formState.zjm" class="w-64" placeholder="姓名/助记码/编号/手机号" />
        </a-form-item>
        <a-form-item name="dateRange" label="登记日期">
          <a-range-picker v-model:value="formState.dateRange" class="w-64" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
        <a-form-item name="isLocal" class="ml-5">
          <a-radio-group v-model:value="formState.isLocal">
            <a-radio value="">全部</a-radio>
            <a-radio value="1">本舰</a-radio>
            <a-radio value="0">外来</a-radio>
          </a-radio-group>
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2">
      <div class="flex">
        <a-button class="mb-2 mr-2" type="primary" @click="onEditOpen"> 新增 </a-button>
        <BaseExport type="防疫防护" name="防疫登记" code="000658" :search-params="formState">导出</BaseExport>
      </div>
      <BaseTable fill :data-source="tableData" :loading="loading" column-code="000658" :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'status'">
            <a-tag v-if="record.status === 0" color="blue">待确认</a-tag>
            <a-tag v-if="record.status === 1" color="green">已确认</a-tag>
            <a-tag v-if="record.status === 2" color="red">已作废</a-tag>
          </template>
          <template v-if="column.dataIndex === 'brName'">
            <PopoverTable :config="detailConfig" :params="{ brId: record.brId }" :width="1200">
              {{ record.brName }}
            </PopoverTable>
          </template>
          <template v-if="column.dataIndex === 'isLocal'">
            {{ record?.isLocal === 1 ? '是' : '否' }}
          </template>
          <template v-if="column.dataIndex === 'isOnShip'">
            {{ record?.isOnShip === 1 ? '是' : '否' }}
          </template>
          <template v-if="column.dataIndex === 'isHighRisk'">
            {{ record?.isHighRisk === 1 ? '是' : '否' }}
          </template>
          <template v-if="column.dataIndex === 'isPositiveHistory'">
            {{ record?.isPositiveHistory === 1 ? '是' : '否' }}
          </template>
          <template v-if="column.dataIndex === 'isOtherUnusual'">
            {{ record?.isOtherUnusual === 1 ? '是' : '否' }}
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-space>
              <a-button type="link" size="small" @click="onEditOpen(record)">修改</a-button>
              <BaseButton confirm-text="是否删除?" type="text" danger size="small" @click="onClickDelete(record, 2)"> 删除 </BaseButton>
              <a-button type="link" size="small" @click="onClickRegistr(record)">疫苗登记</a-button>
              <a-button v-if="record.isOnShip" type="link" size="small" @click="onClickShip(record, 0)"> 在舰 </a-button>
              <a-button v-else type="link" size="small" @click="onClickShip(record, 1)"> 离舰 </a-button>
            </a-space>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <Edit v-model:open="editOpen" :row-data="rowData" @success="getData" />
    <Registration v-model:open="registrationOpen" :row-data="rowData" @success="getData" />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
import { post } from '@/utils/request'
import BaseTable from '@/components/BaseTable'
// import useUserStore from '@/store/user'
import BaseButton from '@/components/BaseButton'
import Registration from './components/VaccinumRegistration'
import Edit from './components/Edit'

const editOpen = ref(false)
const registrationOpen = ref(false)
const rowData = ref({})
const formRef = ref(null)
// const { empId } = useUserStore()
const formState = ref({
  dateRange: [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
  isLocal: ''
})

const { loading, tableData, pagination, getData } = useTable({
  url: '/war/epidemicPrevention/getEpidemicRegistrationResponseList',
  immediate: true,
  formatParams: () => {
    return {
      ...formState.value,
      startDate: formState.value.dateRange?.[0],
      endDate: formState.value.dateRange?.[1]
    }
  }
})

const detailConfig = ref({
  url: '/war/epidemicPrevention/getEpidemicRegistrationVaccinResponseList',
  columnCode: '000659',
  rowKey: 'lsh',
  formatResult: (data) => {
    return data.map((item, index) => ({
      ...item,
      xh: index + 1
    }))
  }
})

const onEditOpen = (record = {}) => {
  rowData.value = { ...record }
  editOpen.value = true
}

// 疫苗登记
const onClickRegistr = (record = {}) => {
  rowData.value = { ...record }
  registrationOpen.value = true
}

// 删除
const onClickDelete = async (record) => {
  const res = await post('/war/epidemicPrevention/doDelEpidemicRegistration', { id: record.id })
  if (res.code !== 0) return message.error(res.msg)
  message.success(res.msg)
  getData()
}

//在舰和离舰
const onClickShip = async (record, status) => {
  const res = await post('/war/epidemicPrevention/doSaveEpidemicRegistration', { id: record.id, isOnShip: status })
  if (res.code !== 0) return message.error(res.msg)
  message.success(res.msg)
  getData()
}
</script>
